package com.sjgs.gis.config;

import io.netty.channel.ChannelOption;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.stereotype.Component;
import reactor.netty.resources.LoopResources;

@Component
public class NettyServerCustomizer extends ReactiveWebServerFactoryCustomizer {
    public NettyServerCustomizer(ServerProperties serverProperties) {
        super(serverProperties);
    }

    @Override
    public void customize(ConfigurableReactiveWebServerFactory factory) {
        NettyReactiveWebServerFactory nettyFactory = (NettyReactiveWebServerFactory) factory;
        nettyFactory.setResourceFactory(null);
        nettyFactory.addServerCustomizers(server ->
                server.tcpConfiguration(tcpServer ->
                        tcpServer.runOn(LoopResources.create("server", 4, 20, true))
                                .selectorOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000).option(ChannelOption.TCP_NODELAY, true)
                ));
    }
}
